export default {
    data() {
        return {
            tableHeight: '100%',
            loading: false,
            columns: [],
            // 表格数据
            tableData: [],
            selectOptions: [],
            // 分页
            pageNo: 1,
            pageSize: 10,
            pageSizes: [10, 30, 50, 100],
            totalSize: 0,
            condition: {
            },
            // 弹窗
            dialogText: '',
            dialogValue: false,
            currentDetail: {},
            ruleForm: {},
            resetForm: {},
            rules: {},
            // 方法
            request: null
        }
    },
    computed: {
        title: function() {
            return (this.currentDetail ? '编辑' : '添加') + this.dialogText
        },
        emptyText: function() {
            return this.loading ? ' ' : '暂无数据'
        }
    },
    created() {
        this.initSetting()
        this.initData()
    },
    methods: {
        /**
         *  初始化设置 在获取分页数据前调用
         */
        initSetting() {
        },
        initData() {
            if (!this.request || this.loading) return
            if (this.request.page) {
                // this.loading = true
                this.request.page(this.getQueryParams()).then(res => {
                    this.loading = false
                    this.totalSize = res.total
                    this.tableData = res.rows
                    /**
                     *  判断组件使用者是否传入格式化数据方法
                     */
                    this.tableData.forEach(el => {
                        if (el.createTime) {
                            el.createTime = this.dateFormatter(el.createTime)
                        }
                        if (el.scenarioCreateTime) {
                            el.scenarioCreateTime = this.dateFormatter(el.scenarioCreateTime)
                        }
                    })
                    if (this.formatTableData) {
                        this.formatTableData(this.tableData)
                    }
                }).catch(() => this.loading = false)
            }
        },
        formatTableData(tableData) {
        },
        dateFormatter(value) {
            let date = new Date(value);
            let y = date.getFullYear();
            let m = date.getMonth() + 1;
            m = m < 10 ? '0' + m : m;
            let d = date.getDate();
            d = d < 10 ? '0' + d : d;
            let h = date.getHours();
            h = h < 10 ? '0' + h : h;
            let minute = date.getMinutes();
            minute = minute < 10 ? '0' + minute : minute;
            let second = date.getSeconds();
            second = second < 10 ? '0' + second : second;
            return y + '/' + m + '/' + d + ' ' + h + ':' + minute + ':' + second;
        },
        getQueryParams() {
            let params = {
                pageNo: this.pageNo,
                pageSize: this.pageSize
            }
            if (!this.condition) return params
            for (let conditionKey in this.condition) {
                if (conditionKey === 'timeValue') {
                    if (!this.condition[conditionKey] || !this.condition[conditionKey].length) {
                        this.condition[conditionKey] = ['', '']
                    }
                    params['timeStart'] = this.condition.timeValue[0] || undefined
                    params['timeEnd'] = this.condition.timeValue[1] || undefined
                } else {
                    params[conditionKey] = this.condition[conditionKey]
                }
            }
            return params
        },
        onSearch() {
            this.pageNo = 1
            this.initData()
        },
        onReset() {
            for (let key in this.condition) {
                switch (key) {
                    default :
                        this.condition[key] = ''
                }
            }
        },
        onSelect(options) {
            this.selectOptions = options
        },
        onDelete(rowItem = null) {
            let idsList = []
            if (rowItem) {
                idsList = [rowItem.id]
            } else {
                idsList= this.selectOptions.map(el => el.id)
            }
            if (!idsList.length) return this.$message.warning(this.$t('base.error.no_select'))
            this.$confirm(this.$t('comm.del'), this.$t('base.tooltip'), {
                confirmButtonText: this.$t('base.button.ok'),
                cancelButtonText: this.$t('base.button.cancel'),
                type: 'warning'
            })
                .then(() => {
                    this.request.delete(idsList).then(res => {
                        this.onSearch()
                    })
                })
                .catch(() => {
                })
        },
        changePageNo(pageNo) {
            this.pageNo = pageNo
            this.initData()
        },
        changePageSize(pageSize) {
            this.pageSize = pageSize
            this.pageNo = 1
            this.initData()
        },
        dialogEdit(item, dialogValue = true) {
            this.currentDetail = item
            for (const ruleFormKey in this.ruleForm) {
                this.ruleForm[ruleFormKey] = item[ruleFormKey]
            }
            this.dialogValue = dialogValue
        },
        emitSave() {
            this.$refs.formRef.validate(valid => {
                if (valid) {
                    this.request[this.ruleForm.id ? 'update' : 'create']({ ...this.ruleForm }).then(res => {
                        this.initData()
                        this.emitClose()
                    })
                }
            })
        },
        emitClose() {
            this.dialogValue = false
        }
    }
}
